package org.Leetcode;

import java.util.HashMap;

public class E02Leetcode3 {
    /*
        要点：动态规划
    */
    public int lengthOfLongestSubstring(String s){
        int begin = 0;
        HashMap<Character,Integer> result = new HashMap<>();
        int length = 0;
        for (int end = 0; end < s.length(); end++) {
            char value = s.charAt(end);
            if (result.containsKey(value)){     //重复时调整 begin
                begin = Integer.max(begin,result.get(value) + 1);
                result.remove(value);
                result.put(value, end);
            }else {     //不重复
                result.put(value,end);
            }
            length = Integer.max(length,end - begin + 1);
        }

        return length;
    }

    public static void main(String[] args) {
        System.out.println(new E02Leetcode3().lengthOfLongestSubstring("abba"));
    }
}
